from src.model import *
from src.utils import *
from keras.optimizers import SGD
import numpy as np

# 尝试跑自己的小网络
if __name__ == '__main__':
    test_filename = r'H:\wangjianlian\data\formal_data\HER2\thrid_generation\val\20X\g3'
    batch_size = 50

    selfModel = SelfModel()
    model = selfModel.build(input_shape = (256, 256, 3))
    # model = selfModel.build(input_shape = (256, 256, 1)) # 边缘图
    model.compile(optimizer=SGD(lr=0.00000005, momentum=0.9, nesterov=True),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model_path = r'H:\wangjianlian\project\Python\networkTest\resources\weight\temp\self_model\2model_1.h5'
    model.load_weights(model_path)

    data = Data()
    all_loss = 0
    all_metrics = 0
    for index, tested_imgs, tested_label in data.read_image(test_filename, batch_size, shuffle = False):
        x_test = tested_imgs
        # x_test = np.expand_dims(x_test, axis=3)  # 增维，仅对边缘图
        y_test = tested_label[0]
        loss_and_metrics = model.evaluate(x_test, y_test, batch_size=batch_size)  # 应该是返回损失值和metrics
        print("测试第%d批，loss值:%f" % (index, loss_and_metrics[0]))
        print("测试第%d批，metrics值:%f" % (index, loss_and_metrics[1]))
        all_loss += loss_and_metrics[0]
        all_metrics += loss_and_metrics[1]

    print("************ 测试：平均值 *************")
    print("测试平均loss值:%f" % (all_loss / index))
    print("测试平均metrics值:%f" % (all_metrics / index))

